package com.yunzong.iot.mqtt;

import android.content.Context;

import org.eclipse.paho.android.service.MqttAndroidClient;
import org.eclipse.paho.client.mqttv3.IMqttAsyncClient;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttMessage;

/**
 * mqttcleint 用来构建mqtt
 * 使用构建者模式
 * 此对象用来管理
 * @author 克林
 */
public class MqttCleint  {
    Builder builder;


    private boolean canDoConnect = true;

    private MqttAndroidClient client;
    private MqttConnectOptions conOpt;

    private Context context;
    private String serverUrl = "";
    private String userName = "admin";
    private String passWord = "password";
    private String clientId = "";
    private int timeOut = 20;
    private int keepAliveInterval = 20;
    private boolean retained = false;
    private boolean cleanSession = false;
    private boolean autoReconnect = true;


    private MqttCallback mqttCallback = new MqttCallback() {

        @Override
        public void connectionLost(Throwable cause) {
//            if (starMQTTCallBack != null) {
//                starMQTTCallBack.connectionLost(arg0);
//            }
        }

        @Override
        public void messageArrived(String topic, MqttMessage message) throws Exception {

        }

        @Override
        public void deliveryComplete(IMqttDeliveryToken token) {

        }
    };

    /**
     * 构造器
     * @param builder
     */
    private MqttCleint(Builder builder) {
        this.context = builder.context;
        this.serverUrl = builder.serverUrl;
        this.userName = builder.userName;
        this.passWord = builder.passWord;
        this.clientId = builder.clientId;

        try {
            this.timeOut = Integer.parseInt(builder.timeOut);
        }catch (Exception e){

        }
        try {
            this.keepAliveInterval = Integer.parseInt(builder.keepAliveInterval) ;
        }catch (Exception e){

        }

        this.retained = builder.retained;

        this.cleanSession = "true".equals(builder.cleanSession);

        this.autoReconnect = builder.autoReconnect;

        init();
    }

    /**
     * 构建者调用bulid 直接创建出一个MqttAndroidClient
     * 它是一个广播接收器
     */
    private void init() {

        // 服务器地址（协议+地址+端口号）
        client = new MqttAndroidClient(context, serverUrl, clientId);
        // 设置MQTT监听并且接受消息
        client.setCallback(mqttCallback);

        conOpt = new MqttConnectOptions();
        // 清除缓存
        conOpt.setCleanSession(cleanSession);
        // 设置超时时间，单位：秒
        conOpt.setConnectionTimeout(timeOut);
        // 心跳包发送间隔，单位：秒
        conOpt.setKeepAliveInterval(keepAliveInterval);
        // 用户名
        conOpt.setUserName(userName);
        // 密码
        conOpt.setPassword(passWord.toCharArray());
        conOpt.setAutomaticReconnect(autoReconnect);
    }

    /**
     * 整整创建
     */
    public static final class Builder{

        private Context context;
        /**
         * mqtt 连接的地址
         */
        String serverUrl;

        /**
         * cleintId
         */
        String clientId ;

        String timeOut;

        /**
         * 认证后的用户名
         */
        String userName ;

        /**
         * 认证后的密码
         */
        String passWord;

        /**
         * 是否清除session "true" 为true
         */
        String cleanSession;


        /**
         *
         */
        boolean retained=false;


        /**
         * 是否重连
         */
        private boolean autoReconnect = false;


        /**
         * keepalive 时长
         */
        String keepAliveInterval ;


        public Builder serverUrl(String serverUrl) {
            this.serverUrl = serverUrl;
            return this;
        }

        public Builder userName(String userName) {
            this.userName = userName;
            return this;
        }

        public Builder passWord(String passWord) {
            this.passWord = passWord;
            return this;
        }

        public Builder clientId(String clientId) {
            this.clientId=clientId;
            return this;
        }

        public Builder timeOut(String timeOut) {
            this.timeOut = timeOut;
            return this;
        }

        public Builder keepAliveInterval(String keepAliveInterval) {
            this.keepAliveInterval = keepAliveInterval;
            return this;
        }

        public Builder retained(boolean retained) {
            this.retained = retained;
            return this;
        }

        public Builder autoReconnect(boolean autoReconnect) {
            this.autoReconnect = autoReconnect;
            return this;
        }

        public Builder cleanSession(String cleanSession) {
            this.cleanSession = cleanSession;
            return this;
        }


        public  MqttCleint build(Context context){
            this.context = context;
            return new MqttCleint(this);
        }
    }
}
